﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace Pda_Total_Station
{
    class mygraph
    {
        #region 坐标轴
        double max_x;
        double min_x;
        double ratio_x = 1;
        double max_y = -9999;
        double min_y = 0;
        double ratio_y = 1;
        int W;
        int H;
        #endregion
        public mygraph()
        { }
        private void config_xy(DataTable dt)
        {
            max_x = 0;
            min_x =0;
            max_y=-999;
            min_y=999;
            foreach (DataRow dr in dt.Rows)
            {
                double val = Convert.ToDouble(dr[0]) * 100;
                if (max_y < val)
                {
                    max_y = val;
                }

                if (min_y > val)
                {
                    min_y = val;
                }
            }
            max_y = Math.Floor(max_y) + 2;
            min_y = Math.Floor(min_y) - 2;
            max_x = dt.Rows.Count + 2;
            ratio_x = (W - 20) / (max_x - min_x);
            ratio_y = (H - 20) / (max_y - min_y);
        }

        void xy_screen(double x, double y, ref double sx, ref double sy)
        {
            sy = -(y - min_y) * ratio_y - 10 + H;
            sx = (x - min_x) * ratio_x + 10;

        }
        public Image Render(string title, int width, int height, DataTable chartTable,string name)
        {
            W = width;
            H = height;
            Bitmap bm = new Bitmap(width, height);
            Graphics g = Graphics.FromImage(bm);
            g.Clear(Color.White);
            const int top = 10;
            const int left = 10;
            if (chartTable == null)
            {
                g.DrawString("没有数据源", new Font("Tahoma", 9, FontStyle.Regular), new SolidBrush(Color.Black), 0, 0);
                return bm;
            }

            DataTable dt = chartTable;
            //计算最高的点 
            //获取整个数据的最高最低点
            config_xy(dt);
               //建立一个Graphics对象实例 
            //try
            //{
                //画坐标轴
                //g.DrawString(title, new Font("Tahoma", 12, FontStyle.Regular), new SolidBrush(Color.Black), 2, 2);
                StringFormat drawFormat = new StringFormat();
                drawFormat.FormatFlags = StringFormatFlags.NoClip;
                drawFormat.LineAlignment = StringAlignment.Center;
                g.DrawString("[点号--" + name + "]", new Font("Tahoma", 8, FontStyle.Regular), new SolidBrush(Color.Black),
                    12, top);
                //画X轴,pen,x1,y1,x2,y2 注意图像的原始X轴和Y轴计算是以左上角为原点，向右和向下计算的
                g.DrawLine(new Pen(Color.Black), left, -10 + height, width - 10, -10 + height);
                //画Y轴,pen,x1,y1,x2,y2
                g.DrawLine(new Pen(Color.Black), 10, 10, 10, -10 + height);
                //获取点的个数
                int count = dt.Rows.Count;
                Point[] pts = new Point[count];
                for (int i = 0; i < count; i++)
                {
                    double y = Convert.ToDouble(dt.Rows[i][0]) * 100;
                    double x = i + 1;
                    double sx, sy;
                    sx = 0; sy = 0;
                    xy_screen(x, y, ref sx, ref sy);
                    Point p = new Point(Convert.ToInt32(sx),Convert.ToInt32( sy));
                    pts[i] = p;
                    g.FillEllipse(new SolidBrush(Color.BlueViolet), p.X - Convert.ToInt32(ratio_x / 16), p.Y - Convert.ToInt32(ratio_y / 16)
                        , Convert.ToInt32(ratio_x / 8), Convert.ToInt32(ratio_y / 8));
                    g.DrawString(dt.Rows[i][0].ToString(), new Font("Tahoma", 6, FontStyle.Regular), new SolidBrush(Color.Black),
                p.X, p.Y - Convert.ToInt32(ratio_y / 2));
                    DateTime time = Convert.ToDateTime(dt.Rows[i][1]);
                    string str = time.Month + "\r\n月\r\n" + time.Day + "\r\n日\r\n" + time.Hour + "\r\n时\r\n" + time.Minute + "\r\n分";
                    g.DrawString(str, new Font("Tahoma", 6, FontStyle.Regular), new SolidBrush(Color.Brown), p.X, p.Y + Convert.ToInt32(ratio_y / 2));
                }
                g.DrawLines(new Pen(Color.BlueViolet), pts);
                return bm;

            //}
            //catch
            //{
            //    MessageBox.Show("有异常");
            //    return bm;
            //}



        }

    }
}
